<template>
  <el-card>
    <el-tabs>
      <el-tab-pane label="登录账户设置">
        <el-form
          ref="formData"
          :model="list"
          :rules="rules"
          label-width="120px"
          style="margin-top: 30px; margin-left: 120px"
        >
          <el-form-item label="姓名" prop="username">
            <el-input v-model="list.username" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="list.password"
              style="width: 300px"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">保存</el-button>
            <el-button @click="cancel">取消</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="个人详情">
        <!-- <user-info></user-info> -->
        <!-- 也可以用动态路由 - 动态路由最大的作用：通过is属性切换路由 -->
        <component :is="userComponent"></component>
      </el-tab-pane>
      <el-tab-pane label="岗位信息">
        <component :is="jobComponent"></component>
      </el-tab-pane>
    </el-tabs>
  </el-card>
</template>

<script>
import { getUserDetailByIdAPI } from '@/api/user'
import { updateUserInfo } from '@/api/employees'
import UserInfo from './components/user-info'
import JobInfo from './components/job-detail'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userComponent: 'UserInfo',
      jobComponent: 'JobInfo',
      list: {
        username: '',
        password: ''
      },
      id: this.$route.params.id,
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度为6-9位', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    async getUserDetail() {
      this.list = await getUserDetailByIdAPI(this.id)
      console.log(this.list)
    },
    async submitForm() {
      try {
        await this.$refs.formData.validate()
        await updateUserInfo(this.list)
        this.$message.success('修改成功')
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.getUserDetail()
      this.$refs.formData.resetFields()
    }
  }
}
</script>

<style></style>
